package org.tinygroup.dbclustercase;

import java.sql.Connection;
import java.sql.SQLException;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.tinygroup.dbcluster.jdbc.DbclusterDataSource;

public class TestInitial {

	private static TestInitial init;

	private DbclusterDataSource dataSource;

	private DruidDataSource druidDataSource;

	private ClassPathXmlApplicationContext applicationContext;
	
	private TestInitial() {

	}

	public static TestInitial getInstance() {
		if (init == null) {
			init = new TestInitial();
		}
		return init;
	}

	public void init() {
		this.applicationContext=new ClassPathXmlApplicationContext("dbcluster.xml");
		applicationContext.refresh();
		applicationContext.registerShutdownHook();
		this.dataSource=(DbclusterDataSource) applicationContext.getBean("shardingDatasource");
		this.druidDataSource=(DruidDataSource) applicationContext.getBean("dataSource");
	}

	public Connection getDbclusterConnection() throws SQLException{
		return dataSource.getConnection();
	}
	
	public Connection getCommonConnection() throws SQLException{
		return druidDataSource.getConnection();
	}

	public DbclusterDataSource getDataSource() {
		return dataSource;
	}

	public DruidDataSource getDruidDatasource() {
		return druidDataSource;
	}
	
}
